﻿using LowCoding.Application.Sys.Right;
using LowCoding.Common.Security;
using LowCoding.Common;
using LowCoding.Dto.Sys;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace LowCoding.API.Controllers.sys.Right
{
    /// <summary>
    /// 权限API
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class SysRightController : BaseController
    {
        private readonly ISysRightService _SysRightservice;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="SysRightservice"></param>
        public SysRightController(ISysRightService SysRightservice)
        {
            _SysRightservice = SysRightservice;
        }

        /// <summary>
        /// 获取主页菜单信息√
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetRoleRightsMod")]
        public async Task<ResponseMessageModel> GetRoleRightsMod()
        {
            //通过GetCurrentUser方法获取token里的UserId
            var userId = AppManager.GetCurrentUser().UserId;
            var result = await _SysRightservice.GetRoleRightsMod(userId);
            //将UserId传递给方法GetRoleRightsMod进行调用
            return Success(result);
        }

        /// <summary>
        /// 根据roleId查询所有的菜单信息并标注权限是否拥有√
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="mdlCode">模块代码</param>
        /// <returns></returns>
        [HttpGet("SysRoleRights")]
        public async Task<ResponseMessageModel> SysRoleRights(string roleId,string mdlCode )
        {
            var result = await _SysRightservice.GetSysRoleRights(roleId, mdlCode);
            return Success(result);
        }

        /// <summary>
        /// 更新角色权限√
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("update")]
        public async Task<ResponseMessageModel> SysRightUpdate([FromForm] SysRightInput input)
        {
            //实例化一个string类型的集合
            List<string> array = new List<string>();
            //判断如果权限ID不为空就将它以逗号分割存放在array集合中
            if (!string.IsNullOrEmpty(input.rights))
            {
                array = input.rights.Split(',').ToList();
            }
            //将角色ID和array集合赋予SysRightsUpdate方法进行传值
            var result = await _SysRightservice.SysRightUpdate(input.roleid, array);
            return Success(result);
        }


    }
}
